package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.operate.trading.api.parameter.CreateTradingIdByOldTradingIdBean;
import com.ymatou.operate.trading.api.service.CreateTradingIdByOldTradingIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 根据老的交易号生成新的交易号（用于合并支付） FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_CreateTradingIdByOldTradingId {

	private static CreateTradingIdByOldTradingIdBean ctidotbean;
	private static CreateTradingIdByOldTradingIdCall ctidotcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据老的交易号生成新的交易号（用于合并支付）");
	}

	@Before
	public void caseUp() {

		ctidotbean = new CreateTradingIdByOldTradingIdBean();
		ctidotcall = new CreateTradingIdByOldTradingIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(ctidotcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	//该接口即将废弃
	@SuppressWarnings({ "rawtypes", "deprecation" })
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_CreateTradingIdByOldTradingId_001() {
		Logger.start(true,"根据老的交易号生成新的交易号（用于合并支付）成功");
		try {
			// 创建订单
			Map map = new OrderCallService().createBatchM2cOrder(1);		
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");

			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额		
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);	
			// 准备参数
			ctidotbean.setOldTradingId(tradingId);
			ctidotbean.setUserId(userId);
			// 发送请求
			ctidotcall.setData(ctidotbean);
			ctidotcall.callService();
			Logger.comment("ctidotcall send:" + ctidotcall.getEntityString());
			Logger.comment("ctidotcall response:" + ctidotcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, ctidotcall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG2, ctidotcall.getMsg(),
			// "验证Msg");
			Logger.verifyEquals(true, ctidotcall.getNewTradingId() > 0, "验证新生成的交易ID");
			// 数据库验证
			DefaultWapper database = new DefaultWapper();
			List<Map> itemlist = database.selectTradingitemByTradingId(ctidotcall.getNewTradingId());
			List<Map> infolist = database.selectTradinginfoByTradingId(ctidotcall.getNewTradingId());
			Logger.verifyEquals(1, itemlist.size(), "验证Ymt_Tradingitem增加一条记录");
			Logger.verifyEquals(1, infolist.size(), "验证Ymt_Tradinginfo增加一条记录");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}